package com.logistics.reception.controller;

import com.logistics.erp.entity.model.*;
import com.logistics.flow.service.FlowService;
import com.logistics.reception.service.ReceptionService;
import com.logistics.utils.aspect.annotation.LoggerListener;
import com.logistics.utils.constants.LoggerMethod;
import com.logistics.utils.exception.MessageException;
import com.logistics.utils.response.ResponseUtil;
import com.logistics.utils.tool.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 外协接收 前端控制器
 * </p>
 * @author guangyuan.tang
 * @since 2024-00-19
 */
@RestController
@RequestMapping("lgs/reception")
public class ReceptionController {

    @Autowired
    private ReceptionService receptionService;

    @Autowired
    private FlowService flowService;

    /**
     * 查询物料单据信息
     * @param code
     * @return
     */
    @GetMapping("/materialFlow/{code}")
    @LoggerListener(businessName = "外协接收-查询物料单据信息", methodName = LoggerMethod.SEARCH)
    public ResponseEntity materialFlow(@PathVariable("code") String code) throws MessageException {
        return ResponseUtil.success(receptionService.materialFlow(code));
    }

    /**
     * 外协接收
     * @param erpReceptionSave
     * @return
     */
    @PutMapping("/save")
    @LoggerListener(businessName = "外协接收", methodName = LoggerMethod.SAVE)
    public ResponseEntity save(@RequestBody ErpReceptionSave erpReceptionSave) throws MessageException {
        return ResponseUtil.success(receptionService.save(erpReceptionSave));
    }


    /**
     * 直流
     * @param code
     */
    @GetMapping("/direct/{code}")
    @LoggerListener(businessName = "直流", methodName = LoggerMethod.SEARCH)
    public ResponseEntity direct(@PathVariable("code") String code) throws Exception{
        MaterialFlowRow flowRow = flowService.materialFlow(code);
        ErpDirectSave erpDirectSave = ErpDirectSave.build(flowRow);
        flowService.save(erpDirectSave);
        return ResponseUtil.success(flowRow);
    }


    /**
     * 周转
     * @param erpTakeOverSave
     * @return
     * @throws Exception
     */
    @PutMapping("/turnover")
    @LoggerListener(businessName = "周转", methodName = LoggerMethod.SEARCH)
    public ResponseEntity turnover(@RequestBody ErpTakeOverSave erpTakeOverSave) throws Exception{
        MaterialFlowRow flowRow = flowService.materialFlow(erpTakeOverSave.getTaskNo());
        flowService.insertTakeOver(erpTakeOverSave);
        return ResponseUtil.success(flowRow);
    }

    /**
     * 获取打印查询任务类型数据
     * @param code
     */
    @GetMapping("/print/{code}")
    @LoggerListener(businessName = "入库打印信息查询", methodName = LoggerMethod.SEARCH)
    public ResponseEntity print(@PathVariable("code") String code) throws Exception{
        String type = receptionService.print(code);
        if (StringUtils.isEquals(type,"外协回厂") || StringUtils.isEquals(type,"外协回厂直流")){
            return ResponseUtil.success(flowService.materialFlow(code));
        } else if (StringUtils.isEquals(type,"外协完工入库")) {
            return ResponseUtil.success(receptionService.inbound(code));
        }else {
            throw new MessageException("外协过程确认失败");
        }
    }

}

